package com.hp.attendance.service;

import com.hp.attendance.dao.EmployeeDao;
import com.hp.attendance.entity.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class EmployeeService {

    private EmployeeDao employeeDao = new EmployeeDao();

    /**
     * 登录校验
     * @param employeeNumber
     * @param password
     */
    public Employee checkLogin(String employeeNumber ,String password) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Employee employee = employeeDao.getEmployee(employeeNumber, password);
        return employee;
    }
    public PageInfo<Employee> getPage(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<Employee> page = new PageInfo<Employee>();
        //查询分页的数据                           2       10
        List<Employee> list = employeeDao.getLimitList(pageNo, pageSize);
        page.setRecords(list);
        //查询总记录条数
        long total = employeeDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }


    //执行分配角色
    public void distriRole(List<EmpRole> listrm, int roleId){
        try {
            //开启事务
            employeeDao.delEmpRole(roleId);
            employeeDao.addEmpRole(listrm);
            //提交事务
        } catch (Exception throwables) {
            //回滚事务
            throwables.printStackTrace();
        }
    }

    public Employee queryEmployee(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return employeeDao.queryEmployee(id);
    }



    public void oneselfUpdate(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        employeeDao.oneselfUpdate(employee);
    }


    public void empUpdate(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        employeeDao.empUpdate(employee);
    }



    //获取所有emp表员工信息
    public List<Employee> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return employeeDao.getList();
    }
    public Employee getEmployeeByEmpNum(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return employeeDao.getEmployeeByEmpNum(id);
    }
    //根据id删除emp表员工信息
    public void empDelete(int id) throws SQLException, IOException, ClassNotFoundException {
        employeeDao.empDelete(id);
    }

    //添加emp表员工信息
    public void empAdd(Employee employee) throws SQLException, IOException, ClassNotFoundException {
        employeeDao.empAdd(employee);
    }
}
